package com.example.hao.flowercityfm.HFAdapter.ViewHolder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.hao.flowercityfm.R;
import com.example.hao.flowercityfm.application.MyApp;
import com.example.hao.flowercityfm.pojo.Type4ListItem;
import com.example.hao.flowercityfm.pojo.Type5ListItem;
import com.example.hao.flowercityfm.utils.HttpUtils;
import com.google.gson.Gson;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Type5ViewHolder extends BaseTypeViewHolder {

    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.title)
    TextView title;

    private Type5RvAdapter type5RvAdapter;

    public Type5ViewHolder(Context context, View itemView) {
        super(context, itemView);
        initView();
    }

    @Override
    public void initView() {
        title.setText("主持人");
        rv.setLayoutManager(new GridLayoutManager(context, 3));
        type5RvAdapter = new Type5RvAdapter(context);
        rv.setAdapter(type5RvAdapter);
        rv.addItemDecoration(new ItemDecoration(context.getResources().getDimensionPixelOffset(R.dimen._5dp)));
        loadData(URL_17);
    }

    private static final String URL_17 = "https://api.myjson.com/bins/ijfhs";

    @Override
    void loadData(String url) {
        HttpUtils.method2(url, new HttpUtils.OnOkCallBack() {
            @Override
            public void onFailure(@NonNull IOException e) {
                Log.e(MyApp.TAG, "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(@NonNull String jsonStr) {
                Type5ListItem type5ListItem = new Gson().fromJson(jsonStr, Type5ListItem.class);
                type5RvAdapter.setItem(type5ListItem);
            }
        });
    }

    class Type5RvAdapter extends RecyclerView.Adapter<Type5RvAdapter.ViewHolder> {

        private Context context;
        private Type5ListItem type5ListItem;

        public Type5RvAdapter(Context context) {
            this.context = context;
        }

        public void setItem(Type5ListItem type4ListItem) {
            this.type5ListItem = type4ListItem;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.fragment_home_type5_item, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Type5ListItem.DataBean.PageListBean pageListBean = type5ListItem.getData().getPageList().get(position);
            holder.tvName.setText(pageListBean.getNickname());
            holder.tvInformation.setText(pageListBean.getDescription());
            Glide.with(context).load(pageListBean.getPublicImage()).into(holder.ivBg);
        }

        @Override
        public int getItemCount() {
            return type5ListItem == null ? 0 : type5ListItem.getData().getPageList().size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.iv_bg)
            ImageView ivBg;
            @BindView(R.id.tv_name)
            TextView tvName;
            @BindView(R.id.tv_information)
            TextView tvInformation;

            public ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
